package fourthQuestion;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private static String path="E:/蒋金鑫/传过来的文件/过年了.";

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        BufferedReader br = null;
        PrintWriter pw =null;

        try {
            serverSocket = new ServerSocket(8000);
            System.out.println("服务端已经启动。。。等待客户端连接。。。"+serverSocket.getInetAddress());
            Socket socket=serverSocket.accept();
            System.out.println("客户端已经连接上了...");

            is = socket.getInputStream();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream(),true);


            String info=br.readLine();
            System.out.println(info);

            StringBuffer sb = new StringBuffer();
            sb.append(path);
            if (info.contains("MP4")){
                sb.append("MP4");
                fos = new FileOutputStream(new File(sb.toString()));
            }else if (info.contains("JPG")){
                sb.append("JPG");
                fos = new FileOutputStream(new File(sb.toString()));
            }

            byte[] bytes = new byte[1024];
            int temp=0;
            while ((temp=is.read(bytes))!=-1){

                fos.write(bytes,0,temp);
                fos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
